package gui;
import Contoller.Main;
import ModelMulti.MonteCarloCalMultiThread;
import ModelSimple.MonteCarloCalSimpleThread;
//import java.awt.Color.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutionException;


	
public  class Interf extends javax.swing.JFrame implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	//Variables declaration -
	private javax.swing.JComboBox Arg1;
    private javax.swing.JComboBox Arg2;
    private javax.swing.JComboBox Arg3;
    private javax.swing.JComboBox Arg4;
    private javax.swing.JComboBox Arg5;
    private javax.swing.JComboBox Arg6;
    private javax.swing.JComboBox Arg7;
    private javax.swing.JComboBox Arg8;
    private javax.swing.JLabel Gain;
    private javax.swing.JLabel Labelarg1;
    private javax.swing.JLabel Labelarg2;
    private javax.swing.JLabel Labelarg3;
    private javax.swing.JLabel Labelarg4;
    private javax.swing.JLabel Labelarg5;
    private javax.swing.JLabel Labelarg6;
    private javax.swing.JLabel Labelarg7;
    private javax.swing.JLabel Labelarg8;
    private javax.swing.JButton Quit;
    private javax.swing.JLabel ResultM;
    private javax.swing.JButton Start;
    private javax.swing.JLabel TimeMulti;
    private javax.swing.JLabel Titre;
    private javax.swing.JLabel jLabel1;
    public javax.swing.JProgressBar jProgressBar1;
    public javax.swing.JProgressBar jProgressBar2;
    private javax.swing.JLabel resltV;
    private javax.swing.JLabel time;
   // End of variables declaration
	private String[][] tab;
    /**
     * Creates new form NewJFrame
     */
	public Interf(String[][] recuperation_information) {
		// TODO Auto-generated constructor stub
		
		//Tableau de valeurs CSV.
		this.tab=recuperation_information;
		
		
		//initialisation SWING
		initComponents();
		
		//Listener des boutons start & quit
		this.Quit.addActionListener(this);
		this.Start.addActionListener(this);
		
		//Chargement des donnée depuis le fichier .csv dans les Liste
		for(int a=0;a<this.tab.length;a++)
		{
			for(int b=0;b<this.tab[a].length;b++)
			{
				switch(a)
				{
				case 0 :Arg1.addItem(this.tab[a][b]);
				break;
				case 1 :Arg2.addItem(this.tab[a][b]);
				break;
				case 2 :Arg3.addItem(this.tab[a][b]);
				break;
				case 3 :Arg4.addItem(this.tab[a][b]);
				break;
				case 4 :Arg5.addItem(this.tab[a][b]);
				break;
				case 5 :Arg6.addItem(this.tab[a][b]);
				break;
				case 6 :Arg7.addItem(this.tab[a][b]);
				break;
				case 7 :Arg8.addItem(this.tab[a][b]);
				break;
				}
			}
		}
        
        
    }

    /**
     *Initialisation des Composant SWING
     */
    
	 private void initComponents() {

	        Arg1 = new javax.swing.JComboBox();
	        Quit = new javax.swing.JButton();
	        Start = new javax.swing.JButton();
	        Labelarg1 = new javax.swing.JLabel();
	        Labelarg2 = new javax.swing.JLabel();
	        Labelarg4 = new javax.swing.JLabel();
	        Arg2 = new javax.swing.JComboBox();
	        Arg3 = new javax.swing.JComboBox();
	        Labelarg5 = new javax.swing.JLabel();
	        Arg4 = new javax.swing.JComboBox();
	        Labelarg3 = new javax.swing.JLabel();
	        Arg5 = new javax.swing.JComboBox();
	        Arg7 = new javax.swing.JComboBox();
	        Arg6 = new javax.swing.JComboBox();
	        Arg8 = new javax.swing.JComboBox();
	        Labelarg6 = new javax.swing.JLabel();
	        Labelarg7 = new javax.swing.JLabel();
	        Labelarg8 = new javax.swing.JLabel();
	        Titre = new javax.swing.JLabel();
	        resltV = new javax.swing.JLabel();
	        time = new javax.swing.JLabel();
	        ResultM = new javax.swing.JLabel();
	        TimeMulti = new javax.swing.JLabel();
	        Gain = new javax.swing.JLabel();
	        jLabel1 = new javax.swing.JLabel();
	        
	      jProgressBar1 = new javax.swing.JProgressBar();
		jProgressBar2 = new javax.swing.JProgressBar();
	        
	        
	        
	        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

	       
	        Quit.setText("Quit");

	        Start.setBackground(new java.awt.Color(255, 255, 255));
	        Start.setText("Start");
	      

	        Labelarg1.setText("StockPrice today");

	        Labelarg2.setText("Strike price");

	        Labelarg4.setText("Time to maturity");

	  
	        Labelarg5.setText("Cost of carry rate");

	  

	        Labelarg3.setText("Discount rate");

	  


	        Labelarg6.setText("Volatility");

	        Labelarg7.setText("nSteps");

	        Labelarg8.setText("nSimulation");

	        Titre.setText("Argument Function");

	        resltV.setText("Result Simple Process : ");

	        time.setText("Exec Time Simple Process :");

	        ResultM.setText("Result Multi Process :");

	        TimeMulti.setText("Exec Time Multi Process :");

	        Gain.setText("Gain :");

	        jLabel1.setText("Result : ");

	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addComponent(jLabel1)
	                .addGap(0, 0, Short.MAX_VALUE))
	            .addGroup(layout.createSequentialGroup()
	                .addGap(23, 23, 23)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addGroup(layout.createSequentialGroup()
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                                .addGroup(layout.createSequentialGroup()
	                                    .addGap(103, 103, 103)
	                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
	                                        .addComponent(Arg1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                                        .addComponent(Labelarg2)
	                                        .addComponent(Arg2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                                        .addComponent(Arg3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                                        .addComponent(Labelarg4)
	                                        .addComponent(Arg4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                                        .addComponent(Labelarg3)))
	                                .addComponent(Quit)
	                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
	                                    .addGap(2, 2, 2)
	                                    .addComponent(Titre)
	                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                                    .addComponent(Labelarg1)))
	                            .addComponent(jProgressBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))
	                        .addGap(48, 48, 48)
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addGroup(layout.createSequentialGroup()
	                                .addComponent(Gain)
	                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	                                .addComponent(Start)
	                                .addContainerGap())
	                            .addGroup(layout.createSequentialGroup()
	                                .addGap(0, 145, Short.MAX_VALUE)
	                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
	                                        .addComponent(Arg5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                                        .addComponent(Arg6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                                        .addComponent(Arg7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                                        .addComponent(Arg8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                                        .addComponent(Labelarg6)
	                                        .addComponent(Labelarg7)
	                                        .addComponent(Labelarg8)
	                                        .addComponent(Labelarg5))
	                                    .addComponent(ResultM)
	                                    .addComponent(TimeMulti)
	                                    .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))
	                                .addGap(54, 54, 54))))
	                    .addGroup(layout.createSequentialGroup()
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addComponent(resltV)
	                            .addComponent(time))
	                        .addContainerGap())))
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addGroup(layout.createSequentialGroup()
	                        .addGap(13, 13, 13)
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addComponent(Labelarg1, javax.swing.GroupLayout.Alignment.TRAILING)
	                            .addComponent(Labelarg5))
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                            .addComponent(Arg1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                            .addComponent(Arg5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                        .addGap(18, 18, 18)
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                            .addComponent(Labelarg2)
	                            .addComponent(Labelarg6))
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                            .addComponent(Arg2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                            .addComponent(Arg6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                        .addGap(15, 15, 15)
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                            .addComponent(Labelarg3)
	                            .addComponent(Labelarg7))
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                            .addComponent(Arg3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                            .addComponent(Arg7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                        .addGap(18, 18, 18)
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                            .addComponent(Labelarg4)
	                            .addComponent(Labelarg8))
	                        .addGap(4, 4, 4)
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                            .addComponent(Arg4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                            .addComponent(Arg8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
	                    .addComponent(Titre))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
	                .addComponent(jLabel1)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(resltV)
	                    .addComponent(ResultM))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(time)
	                    .addComponent(TimeMulti))
	                .addGap(2, 2, 2)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(jProgressBar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
	                        .addGap(2, 2, 2)
	                        .addComponent(Gain)
	                        .addGap(24, 24, 24))
	                    .addGroup(layout.createSequentialGroup()
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                            .addComponent(Start)
	                            .addComponent(Quit))
	                        .addContainerGap())))
	        );

	        pack();
	    }// </editor-fold>


  

    
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
		if(e.getSource()==Quit)//Quit le programme
		{
		System.exit(0);
		}	
		if(e.getSource()==Start)//Lance la simulation
		{
			//Utiliser pour pouvoir continué à modifier la JFRAME d'affichage 
			 Runnable runner = new Runnable()
			 {
			 public void run() {
				 //début calcul time pour Simple thread
				 double Start = System.currentTimeMillis();
				 //Récupération Nb Sim
				 int v = new Integer ((String) Arg8.getSelectedItem());
				
				//On mets les bonnes valeurs max pour la JProgresB
				 Main.GUI.jProgressBar1.setMaximum(v);
				 Main.GUI.jProgressBar2.setMaximum(v);
				 	Main.GUI.jProgressBar1.setValue(0);
				 	Main.GUI.jProgressBar2.setValue(0);
				 	
				 	//calcul Simple thread
					MonteCarloCalSimpleThread Cal = new MonteCarloCalSimpleThread(
							  "c", //String CallPutFlag
							  new Double( (String) Arg1.getSelectedItem()),//double S
							  new Double((String) Arg4.getSelectedItem()),//double T
							  new Double((String)Arg3.getSelectedItem()),//double r
							  new Double((String)Arg5.getSelectedItem()),//double b
							  new Double((String)Arg6.getSelectedItem()),//double v
							  new Integer((String)Arg7.getSelectedItem()),//integer nstep 
							  new Integer((String)Arg8.getSelectedItem()),//integer nSimulation
							  new Double((String)Arg2.getSelectedItem()));//double X
					
					Cal.CalculOneThread();
					
					//Affichage JFRAME value
					resltV.setText("Result Simple Process : "+Cal.getResutl());
					
					Double ExecTime = System.currentTimeMillis()-Start;
					
					time.setText("Exec Time Simple Process : "+ ExecTime.toString() +" ms");
					
					
					
					 try {
						 	//Lancement MultiThread
							MonteCarloCalMultiThread Cal2 = new MonteCarloCalMultiThread(
									  "c", //String CallPutFlag
									  new Double( (String) Arg1.getSelectedItem()),//double S
									  new Double((String) Arg4.getSelectedItem()),//double T
									  new Double((String) Arg3.getSelectedItem()),//double r
									  new Double((String) Arg5.getSelectedItem()),//double b
									  new Double((String) Arg6.getSelectedItem()),//double v
									  new Integer((String) Arg7.getSelectedItem()),//integer nstep 
									  new Integer((String) Arg8.getSelectedItem()),//integer nSimulation
									  new Double((String) Arg2.getSelectedItem()));
							
							//Affichage Multi Value
							ResultM.setText("ResultM :"+Cal2.getResutl());
							TimeMulti.setText("Exec Time Multi : "+Cal2.getTime());
							
							//Calcul Gain Temps Simpe - Multi
							Gain.setText("Exec Time Gain : "+(ExecTime-Cal2.getTime())+"ms");
							
						} catch (NumberFormatException e2) {
							// TODO Auto-generated catch block
							e2.printStackTrace();
						} catch (InterruptedException e2) {
							// TODO Auto-generated catch block
							e2.printStackTrace();
						}
				 catch (ExecutionException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
			 
			 }
			 };
			 Thread thread = new Thread(runner);
			 thread.start();
				
				}
	}

}
